package com.pdg.mcplugin.ranger.dataproviders;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.bukkit.Location;

import com.pdg.mcplugin.common.interfaces.TableProviderValue;

public class PlaceNameLocation implements TableProviderValue<PlaceNameLocation>{
	public static final String FIELD_X = "x";
	public static final String FIELD_Y = "y";
	public static final String FIELD_Z = "z";
	private static final String CREATE_FIELDS = "`"+FIELD_X+"` REAL NOT NULL, `"+
													FIELD_Y+"` REAL NOT NULL, `"+
													FIELD_Z+"` REAL NOT NULL";
	private static final String INSERT_FIELDS = FIELD_X+","+
												FIELD_Y+","+
												FIELD_Z;
	private static final String SELECT_LIST = 	FIELD_X+","+
												FIELD_Y+","+
												FIELD_Z;
	private static final String UPDATE_LIST_FORMAT = "`"+FIELD_X+"`=%f,`"+
														FIELD_Y+"`=%f,`"+
														FIELD_Z+"`=%f";
	private static final String INSERT_VALUES_FORMAT = "%f,%f,%f";
	private static final double DEFAULT_X = 0;
	private static final double DEFAULT_Y = 0;
	private static final double DEFAULT_Z = 0;
	private double x;
	private double y;
	private double z;
	public PlaceNameLocation(double theX,double theY,double theZ){
		setX(theX);
		setY(theY);
		setZ(theZ);
	}
	public double getX() {
		return x;
	}
	private void setX(double x) {
		this.x = x;
	}
	public double getY() {
		return y;
	}
	private void setY(double y) {
		this.y = y;
	}
	public double getZ() {
		return z;
	}
	private void setZ(double z) {
		this.z = z;
	}
	public double getXYZDistance(Location theLocation){
		double theDeltaX = theLocation.getX() - getX();
		double theDeltaY = theLocation.getY() - getY();
		double theDeltaZ = theLocation.getZ() - getZ();
		return Math.sqrt(theDeltaX*theDeltaX+theDeltaY*theDeltaY+theDeltaZ*theDeltaZ);
	}
	public double getXZDistance(Location theLocation){
		double theDeltaX = theLocation.getX() - getX();
		double theDeltaZ = theLocation.getZ() - getZ();
		return Math.sqrt(theDeltaX*theDeltaX+theDeltaZ*theDeltaZ);
	}
	public double getRelativeYaw(Location theLocation){
		double theDeltaX = theLocation.getX() - getX();
		double theDeltaZ = theLocation.getZ() - getZ();
		double theYaw = Math.atan2(theDeltaZ, theDeltaX) * 180 / Math.PI;
		
		double thePlayerYaw = theLocation.getYaw()+90+180;
		while(thePlayerYaw>180.0) thePlayerYaw-=360.0;
		while(thePlayerYaw<=-180.0) thePlayerYaw+=360.0;
		
		theYaw -= thePlayerYaw;
		while(theYaw>180.0) theYaw-=360.0;
		while(theYaw<=-180.0) theYaw+=360.0;
		return theYaw;
	}
	@Override
	public String getCreateFields() {
		return CREATE_FIELDS;
	}
	@Override
	public String getInsertFields() {
		return INSERT_FIELDS;
	}
	@Override
	public String getInsertValues() {
		return String.format(INSERT_VALUES_FORMAT, getX(),getY(),getZ());
	}
	@Override
	public String getSelectList() {
		return SELECT_LIST;
	}
	@Override
	public String getUpdateList() {
		return String.format(UPDATE_LIST_FORMAT, getX(),getY(),getZ());
	}
	@Override
	public PlaceNameLocation createValueFromResultSet(ResultSet theResultSet) {
		try {
			return new PlaceNameLocation(
					theResultSet.getDouble(FIELD_X), 
					theResultSet.getDouble(FIELD_Y), 
					theResultSet.getDouble(FIELD_Z));
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	public static PlaceNameLocation defaultInstance() {
		return new PlaceNameLocation(DEFAULT_X, DEFAULT_Y, DEFAULT_Z);
	}
	
}
